package tg.app.rj.workticket.virtual;

import java.rmi.RemoteException;

import psdi.app.common.virtual.ChangeStatusSet;
import psdi.mbo.Mbo;
import psdi.mbo.MboRemote;
import psdi.mbo.MboServerInterface;
import psdi.mbo.MboSet;
import psdi.mbo.MboSetRemote;
import psdi.mbo.NonPersistentMboSetRemote;
import psdi.mbo.SqlFormat;
import psdi.util.MXException;
import tg.app.rj.workticket.WorkTicketRemote;

/***
 * 
 * @author pzx
 *
 */
public class WTChangeStatusSet extends ChangeStatusSet implements
		NonPersistentMboSetRemote {
	private static final long serialVersionUID = 1L;

	public WTChangeStatusSet(MboServerInterface mboserverinterface)
			throws MXException, RemoteException {
		super(mboserverinterface);
	}

	protected MboSetRemote getMboIntoSet(MboRemote mboremote)
			throws MXException, RemoteException {

		// MboSetRemote mbosetremote = getMboServer().getMboSet("WORKTICKET",getUserInfo());

		// MboSetRemote changeFVSet = getMboServer().getMboSet(mbo.getName(),getUserInfo());
		// String sqlformat = mbo.getName() + "NUM=:" + mbo.getName() + "NUM";
		// SqlFormat sqlformat = new SqlFormat(mbo, sql);

		// SqlFormat sqlformat = new SqlFormat(mboremote, "wtnum = :wtnum");
		// mbosetremote.setWhere(sqlformat.format());
		// return mbosetremote;

		MboSetRemote changeFVSet = getMboServer().getMboSet(mboremote.getName(), getUserInfo());
		String sql = "WTNUM=:WTNUM";
		SqlFormat sqf = new SqlFormat(mboremote, sql);
		changeFVSet.setWhere(sqf.format());
		return changeFVSet;
	}

	protected Mbo getMboInstance(MboSet mboset) throws MXException,
			RemoteException {
		return new WTChangeStatus(mboset);
	}

	protected void changeMboStatus(MboRemote workticket, MboRemote param)
			throws MXException, RemoteException {
		if(workticket instanceof WorkTicketRemote){
			WorkTicketRemote curworkticket = (WorkTicketRemote) workticket;
			String status=param.getString("status");
			if("CLOSE".equalsIgnoreCase(status) || "WZX".equalsIgnoreCase(status) || "CAN".equalsIgnoreCase(status)){
				curworkticket.setValue("historyflag",1,11L);
				curworkticket.setValue("WFACTIVE","0",11L);
			}
			if("INIT".equalsIgnoreCase(status)){
				curworkticket.setValue("WFACTIVE", "1");
			}
			if("PMT".equalsIgnoreCase(status) && "LX".equals(curworkticket.getString("WTTYPE"))){
				curworkticket.setValue("WFACTIVE", "1");
			}
			curworkticket.changeStatus(status,param.getDate("statdate"), param.getString("memo"));
			workticket.setValue("statusdate", param.getDate("statdate"),11L);
			
		}
		
		
	}
}